package jpal.io;

import java.io.File;
import java.io.IOException;

/**
 * In this example we traverse all the files and sub-directories (up to
 * {@code maxDepth}) beginning with the starting directory, and print them to
 * standard out. All {@code IOException}s are printed to standard error.
 * 
 * @author Zhenya Leonov
 */
public class WalkFileTreeExample {

	private static class FileWalkerImpl extends SimpleFileWalker<Void> {

		@Override
		public FileWalker.VisitResult preVisitDirectory(File dir) {
			System.out.println(dir);
			return FileWalker.VisitResult.CONTINUE;
		}

		@Override
		public FileWalker.VisitResult visitFile(File file) throws IOException {
			System.out.println(file);
			return FileWalker.VisitResult.CONTINUE;
		}

		@Override
		public FileWalker.VisitResult visitFileFailed(File file, IOException ex)
				throws IOException {
			System.err.println(ex);
			return FileWalker.VisitResult.CONTINUE;
		}
	}

	public static void main(String[] args) throws IOException {

		final FileWalker<Void> walker = new FileWalkerImpl();
		final File start = new File("C:/");
		final int maxDepth = 2;

		MoreFiles.walkFileTree(start, maxDepth, walker);

	}

}
